<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function salesman_distributor_map_list_page()
{
    @$output = drupal_render(drupal_get_form('salesman_distributor_map_list_form'));
    return $output;
}

function salesman_distributor_map_list_form($form, &$form_state)
{
    global $user;
    if(get_user_role_name($user->uid) === 'administrator'){
        $form['juris_salesman'] = _insert_salesman_distributor_selection_form($form, $form_state, 'juris_salesman', $zone_second_key = '', $level = 2);
    }else{
        $form['employee_list'] =  _insert_loginuser_choose_salesman_selection($user);
    }
//    $form['distributor'] = get_all_distributors();
    $form['address_distributor_markup']['#markup'] = '<div class="address-distributor-search">';
    $form['address_distributor'] = _insert_address_distributor_selection_form($form, $form_state, 'address_distributor');
    $form['address_distributor_markup_close']['#markup'] = '</div>';
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}

function salesman_distributor_map_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
        global $user;
        $user_info = _get_user_info();
        $output = '';
        //表头
        $header = array(
            array('data' => '选择', 'class' => ['essential no']),
            array('data' => '销售员', 'class' => ['integrant essential salesman'],'field' => 'salesman_id'),
            array('data' => '经销商', 'class' => ['essential distributor']),
            array('data' => '操作', 'class' => ['essential more']),
        );
        $query = db_select('salesman_distributor_map', 'M')->extend('PagerDefault')->extend('TableSort');
        $query->join('employee', 'E', 'E.employee_id=M.salesman_id');
        $query->join('distributor', 'D', 'D.distributor_id=M.distributor_id');
        $query->fields('M', array('map_id'));
        $query->addField('E', 'employee_name');
        $query->addField('D', 'distributor_name');
        $query->join('addr', 'da', 'da.addr_id=D.addr_id');
        $query->leftJoin('addr', 'A', 'A.addr_id=da.addr_id');
        $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
        $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
        $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
        if (!empty($form_state['values']['county'])) {//县级
            $query->condition('da.county_id', $form_state['values']['county'], '=');
        } elseif (!empty($form_state['values']['city'])) {//市级
            $query->condition('da.city_id', $form_state['values']['city'], '=');
        } elseif (!empty($form_state['values']['province'])) {//省级
            $query->condition('da.province_id', $form_state['values']['province'], '=');
    }
        if (!empty($form_state['values']['juris'])){
            $query->condition('E.juris_id', $form_state['values']['juris'], '=');
        }
        //模糊条件筛选
        if (!empty($form_state['values']['fuzzy_salesman'])) {//模糊地址查询条件
            $query->condition('E.employee_name', '%' . db_like($form_state['values']['fuzzy_salesman']) . '%', 'LIKE');
        }
        if (!empty($form_state['values']['salesman'])) {
            $query->condition('E.employee_id',$form_state['values']['salesman'] );
        }
        if (!empty($form_state['values']['employee_list'])&& $form_state['values']['employee_list'] != "default") {//查询条件
            if ($form_state['values']['employee_list']) {
                $query->condition('M.salesman_id', $form_state['values']['employee_list']);
            }
        }
    if (!empty($form_state['values']['juris'])) {//查询条件
        if ($form_state['values']['juris']) {
            $query->condition('E.juris_id', $form_state['values']['juris']);
        }
    }
    if (!empty($form_state['values']['salesman'])) {//查询条件
        if ($form_state['values']['salesman']) {
            $query->condition('E.employee_id', $form_state['values']['salesman']);
        }
    }
    if(!empty($form_state['values']['distributor'])){
//        $query->condition('M.distributor_id', $form_state['values']['distributor'],'=');
        $query->condition('D.distributor_name', '%'.db_like($form_state['values']['distributor']).'%', 'LIKE');

    }
        if($form_state['values']['employee_list'] == "default" || $form_state['triggering_element']['#value'] != '查询'){
            $query->orderBy('M.timestamp', 'DESC');
            if($user_info['role'] != 'administrator'&& $user_info['role'] != '经销商') {
                $salesmans_ids = get_salesman_by_self($user);
                if(!empty($salesmans_ids)){
                    $query->condition('M.salesman_id', $salesmans_ids, 'in');
                }else{
                    $query->condition('M.salesman_id', $user->uid, '=');
                }
            }
        }
    $query->addField('D', 'distributor_id');
        $salesman_distributor_mapInfo = $query->orderByHeader($header)->execute();

        $rows = array();
        //表内容
        $n = 1;
        foreach ($salesman_distributor_mapInfo as $row) {
            $rows[] = array(
                array('data' => '<input type="checkbox" class="listViewTableCheck" value ="' . $row->map_id . '"/>' . $n++, 'class' => ['integrant essential no']),
                array('data' => $row->employee_name, 'class' => ['integrant goRight essential saleman']),
                array('data' => '<a href="'.base_path().'distributor/edit/' . $row->distributor_id . '?distributor_id='.$row->distributor_id .'" data-toggle="modal" target = "_top">'.$row->distributor_name.'</a>', 'class' => ['integrant essential distributor']),
                array('data' => '<a data-toggle="modal" href="'.base_path().'sales/salesman_distributor_map/edit/' . $row->map_id . '" data-title="修改" class="btn btn-mini">修改</a>', 'class' => 'essential edit'),
            );
        }
        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
        /*	 * *按钮** */
        if(sizeof($rows) > 0) {
            $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
            $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
        }
        $output .= theme('pager');
        return $output;
}

function salesman_distributor_map_delete()
{
    ajax_table_delete('salesman_distributor_map', 'map_id');
}

